<?php
namespace App\Controllers;
use \interop\Container\ContainerInterface as ContainerInterface;
use App\Models\UserModel;
use pdo;
class RoleController extends BaseController{
	public function roleList($request,$response,$args){
        include APP_PATH.'/Views/user/rolelist.html';
	}
	public function roleInfo($request,$response,$args){
		echo "roleInfo";
	}
	public function roleListForm($request,$response,$args){
		$user=new UserModel();
		$draw = $request->getParam('draw');
		$start = $request->getParam('start');
        $length = $request->getParam('length');
		$result=$user->roleList();
		$recordsTotal = 0;
        $recordsTotal = count($result);
        json_encode($result);
        $index=0;
        if($recordsTotal%$length==0){
   	    $recordsFiltered=($recordsTotal/$length)*10;
         }
         else{
   	    $recordsFiltered=($recordsTotal/$length)*10+1;
          }
        for($i=$start;$i<$start+$length;$i++,$index++)
   	     {if(empty($result[$i])){
   		break;
   	    }
   	    $infos[$index]=$result[$i];
        }
//      for($i=0,$index1=$start+1;$i<count($infos);$i++,$index1++){
//		    	$infos[$i]['ID']=$index1;
//	}
   echo json_encode(array(
	"draw" => $draw,
	"recordsTotal" =>$recordsTotal,
	"recordsFiltered" =>$recordsFiltered,  // necessary
	"data" =>$infos // necessary
	));
	}
	public function userRole($request,$response,$args){
		$role=$request->getParam('role');
		$id=$request->getParam('id');
		$user=new UserModel();
		$result=json_encode($user->userRolePower($role,$id));
		var_dump($result);
		if($user->userRolePower($role,$id)){
			$ret=$result;
		}
		else{
			$ret=a;
		}
		return $ret;
	}
	public function roleAddForm($request,$response,$args){
		include APP_PATH.'/Views/user/msg/addRole.html';
	}
	public function roleAdd($request,$response,$args){
		$rolename=$request->getParam('rolename');
		$rolelv=$request->getParam('rolelv');
	    $user=new UserModel();
		if($user->addRole($rolename,$rolelv)){
           	$data=0;
           }
           else{
	    	$data=101;
	    }
	   echo $data;
	}
	public function roleDelete($request,$response,$args){
		$roleID=$request->getParam('roleID');
		$user=new UserModel();
		if($user->roleDelete($roleID)){
			$data=0;
		}
		else{
			$data=101;
		}
		echo $data;
	}
	public function userRoleForm($request,$response,$args){
		$id=$request->getParam('id');
		$user=new UserModel();
		$draw = $request->getParam('draw');
		$start = $request->getParam('start');
        $length = $request->getParam('length');
		$result=$user->userrole($id);
		$recordsTotal = 0;
        $recordsTotal = count($result);
        json_encode($result);
        $index=0;
        if($recordsTotal%$length==0){
   	    $recordsFiltered=($recordsTotal/$length)*10;
         }
         else{
   	    $recordsFiltered=($recordsTotal/$length)*10+1;
          }
        for($i=$start;$i<$start+$length;$i++,$index++)
   	     {if(empty($result[$i])){
   		break;
   	    }
   	    $infos[$index]=$result[$i];
        }
       for($i=0,$index1=$start+1;$i<count($infos);$i++,$index1++){
		    	$infos[$i]['ID']=$index1;
	   }
     echo json_encode(array(
	"draw" => $draw,
	"recordsTotal" =>$recordsTotal,
	"recordsFiltered" =>$recordsFiltered,  // necessary
	"data" =>$infos // necessary
	));		
	}
	public function powerList($request,$response,$args){
		include APP_PATH.'/Views/user/powerlist.html';
	}
	public function powerlistForm($request,$response,$args){
		$id=$request->getParam('id');
		$user=new UserModel();
		$draw = $request->getParam('draw');
		$start = $request->getParam('start');
        $length = $request->getParam('length');
		$result=$user->powerlist($id);
		$recordsTotal = 0;
        $recordsTotal = count($result);
        json_encode($result);
        $index=0;
        if($recordsTotal%$length==0){
   	    $recordsFiltered=($recordsTotal/$length)*10;
         }
         else{
   	    $recordsFiltered=($recordsTotal/$length)*10+1;
          }
       
        for($i=$start;$i<$start+$length;$i++,$index++)
   	     {if(empty($result[$i])){
   		break;
   	    }
   	    $infos[$index]=$result[$i];
        }
         for($i=0,$index1=$start+1;$i<count($data);$i++,$index1++){
		    	$infos[$i]['ID']=$index1;
		}
      echo json_encode(array(
	"draw" => $draw,
	"recordsTotal" =>$recordsTotal,
	"recordsFiltered" =>$recordsFiltered,  // necessary
	"data" =>$infos // necessary
	));		
	}
}